﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Configuration;
using FwProject.Controllers;
using FwProject.EventAdvertising.Dto;
using FwProject.Shop;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Configuration;

namespace FwProject.Web.Shop.Controllers
{
    /// <summary>
    /// 优惠券
    /// </summary>
    public class CouponController : FwProjectControllerBase
    {
        private readonly IShopCouponAppService _shopCouponAppService;
        private readonly IShopUserCouponAppService _shopUserCouponAppService;
        private readonly IShopTypeAppService _shopTypeAppService;

        private readonly IConfigurationRoot _appConfiguration;

        public CouponController(IShopCouponAppService shopCouponAppService, IHostingEnvironment env, IShopUserCouponAppService shopUserCouponAppService, IShopTypeAppService shopTypeAppService)
        {
            _shopCouponAppService = shopCouponAppService;
            _shopUserCouponAppService = shopUserCouponAppService;
            _shopTypeAppService = shopTypeAppService;
            _appConfiguration = env.GetAppConfiguration();
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult CouponUser(Guid id)
        {
            return View(id);
        }


        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetShopCouponData(int page, int limit)
        {
            var data = await _shopCouponAppService.GetShopCouponLayerDataAsync(page, limit);

            return Json(data);
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetShopUserCouponData(int page, int limit, Guid id)
        {
            var data = await _shopUserCouponAppService.GetUserCouponLayerDataAsync(page, limit, id);

            return Json(data);
        }

        #region 发放优惠券

        public async Task<IActionResult> Issue(Guid id)
        {

           var model=   await _shopCouponAppService.GetByIdShopCoupon(id);
            ViewBag.Title = model.CouponTitile;

            return View(id);
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetMemberData(int page, int limit, string keyword)
        {
            var data = await _shopUserCouponAppService.GetMemberLayerDataAsync(page, limit, keyword);

            return Json(data);
        }

        [HttpPost]
        public async Task<IActionResult> SaveShopUserCoupon(Guid CouponId, string Members)
        {
            await _shopUserCouponAppService.BatchSaveShopUserCouponAsync(CouponId, Members);
            return Json(CouponId);
        }
        #endregion



        public async Task<IActionResult> Create()
        {
            var list = await _shopTypeAppService.GetParentListAsync();
            ViewBag.Parent = list.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.TypeName });
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(ShopCouponInput input)
        {
            var id = await _shopCouponAppService.CreateShopAdLocation(input);

            return Json(id);
        }


        public async Task<IActionResult> Edit(Guid id)
        {
            var list = await _shopTypeAppService.GetParentListAsync();
            ViewBag.Parent = list.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.TypeName });
            ViewBag.Id = id;
            var entity = await _shopCouponAppService.GetByIdShopCoupon(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Guid id, ShopCouponInput input)
        {
            await _shopCouponAppService.EditShopAdLocation(id, input);
            return Json(id);
        }


        [HttpPost]
        public IActionResult UpLoadImg()
        {
            string FileServicePath = _appConfiguration["FileServicePath:TempCoupon"].ToString();
            return Json(base.UploadFile(FileServicePath));
        }
    }
}